package com.tywj.buscustomerapp.view.fragment;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.BridgeWebViewClient;
import com.tywj.buscustomerapp.Config;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.presenter.BasePresenter;
import com.tywj.buscustomerapp.utils.MD5Utils;
import com.tywj.buscustomerapp.utils.SPUtils;
import com.tywj.buscustomerapp.view.activity.JsWebActivity;
import com.tywj.buscustomerapp.view.activity.LoginActivity;
import com.tywj.buscustomerapp.view.myview.TaylorWebChromeClient;
import com.tywj.buscustomerapp.view.myview.TaylorWebView;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;


public class OutDoorFragment extends BaseMvpFragment {
    private TaylorWebView bridgeWebView;
    private String home="";

    private static OutDoorFragment fragment = new OutDoorFragment();
    public static OutDoorFragment getInstance() {
        return fragment;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_school;
    }

    @Override
    public void initView(View view) {
        bridgeWebView = view.findViewById(R.id.bridge_web);
//        bridgeWebView.setWebChromeClient(new WebChromeClient());
        bridgeWebView.setWebChromeClient(new TaylorWebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                if (message.equals("未登录，请登录")){
                    String user =  SPUtils.getUserPhone(view.getContext());
                    if (user!=null && !user.equals("")){
                        doLogin();
                    }else{
                        Intent loginIntent = new Intent(getActivity(),LoginActivity.class);
                        startActivityForResult(loginIntent,20010);
                    }
                    result.cancel();
                    return true;
                }else{
                    return super.onJsAlert(view, url, message, result);
                }
            }
        });
        bridgeWebView.setWebViewClient( new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    Log.i("taylorWEB",request.getUrl().toString());
                    if ("/od/".equals(request.getUrl().toString().substring(23,27))){
                        view.loadUrl(request.getUrl().toString());
                        Log.i("OUT_DOOR","1");
                        return true;
                    }else if (request.getUrl().toString().equals("")){

                    }else{
                        Intent intent = new Intent(getActivity(),JsWebActivity.class);
                        intent.putExtra("url",request.getUrl().toString());
                        getActivity().startActivity(intent);
                        view.loadUrl(home);
                        return true;
                    }
                }
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
        bridgeWebView.setOnKeyListener(new View.OnKeyListener() {

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((keyCode == KeyEvent.KEYCODE_BACK) && bridgeWebView.canGoBack()) {
                    bridgeWebView.goBack();
                    return true;
                }
                return false;
            }

        });
    }

    private void initClick() {
    }

    @Override
    public void initData() {
        doLogin();
    }

    private void doLogin(){
        StringBuffer path = new StringBuffer(Config.OUT_DOOR);
        String phone = SPUtils.getUserPhone(getActivity().getApplicationContext());
        path.append(Config.OUT_DOOR_ID).append("-").append(phone).append("-")
                .append(MD5Utils.strToMd5By32(Config.OUT_DOOR_ID+phone+Config.OUT_DOOR_SECRET));
        home = path.toString();
        bridgeWebView.loadUrl(home);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode==20010){
            doLogin();
        }
    }

    @Override
    protected BasePresenter bindPresenter() {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("主页—自由行");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("主页-自由行");
    }
}
